Component({
  properties: {
    queId: {
      type: String,
      value: ''
    },
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    queTitle: {
      type: String,
      value: ''
    },
    queOption: {
      type: Array,
      value: [
        'A.问答答案一',
        'B.问答答案二',
        'C.问答答案三',
        'D.问答答案四'
      ]
    },
    answer: {
      type: String,
      value: 'A.问答答案一'
    },
    isSingle: {
      type: Boolean,
      value: true
    }
  },
  data: {
    // 这里是一些组件内部数据
    title: '选择题',
    queMultipleOption: [],
    loading: false,
    btnText: '下一题',
    currIndex: -1,
    currAnswer: '',
    currMultipleIndex: [],
    currMultipleAnswer: [],
    isTrue: false,
    showTip: false
  },
  attached: function () {
    //是否到最后一题
    const total = wx.getStorageSync('totalQuestion')
    const currMod = wx.getStorageSync('modNum')
    this.setData({
      btnText: total === (currMod + 1) ? '提交回答' : '下一题'
    })
  },
  methods: {
    compareArraySort: function(a1, a2) {
      a2 = JSON.parse(a2)
      if (a1.length !== a2.length) return false;
      a1 = [].concat(a1);
      a2 = [].concat(a2);
      a1 = a1.sort();
      a2 = a2.sort();
      for (var i = 0, n = a1.length; i < n; i++) {
        if (a1[i] !== a2[i]) return false;
      }
      return true;
    },
    // 这里是一个自定义方法
    onStep: function (e) {
      const testMode = wx.getStorageSync('testMode')
      const modNum = wx.getStorageSync('modNum')
      const total = wx.getStorageSync('totalQuestion')

      if (testMode === 1) { //练习模式，答完直接判断
        if (this.data.isSingle) {
          if (this.data.currAnswer === '') {
            wx.showToast({
              title: '请选择答案!',
              icon: 'none',
              duration: 2000
            })
            return
          }
        } else {
          if (this.data.currMultipleAnswer.length === 0) {
            wx.showToast({
              title: '请选择答案!',
              icon: 'none',
              duration: 2000
            })
            return
          }
        }
        
      } 
      if (testMode === 2) { //考试模式

      }
      
      //考试模式提交答案
      const daAn = wx.getStorageSync('totalAnswer')
      daAn.push({
        id: this.data.queId,
        answer: this.data.isSingle ? this.data.currAnswer : this.data.currMultipleAnswer,
        time_spent: 0
      })
      wx.setStorageSync('totalAnswer', daAn)
      //统计事件，记录答题是否完成
      this.triggerEvent('myevent', {
        value: modNum + 1,
        complete: total === (modNum + 1)
      }, {})
    },
    choiceAnswer: function (e) {
      const id = this.data.isSingle ? parseInt(e.detail.value) : e.detail.value
      const testMode = wx.getStorageSync('testMode')
      const modNum = wx.getStorageSync('modNum')
      
      if (this.data.isSingle) { //单选
        this.setData({
          currIndex: id,
          currAnswer: this.data.queOption[id]
        })
        if (testMode === 1) { //单选练习模式
          this.setData({ //设置直接显示答案
            showTip: true
          })
          if (this.data.currAnswer === '') {
            wx.showToast({
              title: '请选择答案!',
              icon: 'none',
              duration: 2000
            })
            return
          }
          this.setData({
            isTrue: this.data.currAnswer === this.data.answer
          })
        } else { //单选考试模式
          this.setData({ //设置隐藏答案
            showTip: false
          })
        }
        
      } else { //多选
        if (id === NaN) {
          this.setData({
            currMultipleIndex: [],
            currMultipleAnswer: []
          })
        } else {
          const arryAnswer = []
          for (let i = 0; i < id.length; i += 1) {
            arryAnswer.push(this.data.queOption[id[i]])
          }
          this.setData({
            currMultipleIndex: id.map((v) => {
              return parseInt(v)
            }),
            currMultipleAnswer: arryAnswer
          })
          if (this.data.currMultipleAnswer.length === 0) {
            wx.showToast({
              title: '请选择答案!',
              icon: 'none',
              duration: 2000
            })
            return
          }
        }
        if (testMode === 1) { //多选练习模式
          this.setData({
            showTip: true,
            isTrue: this.compareArraySort(this.data.currMultipleAnswer, this.data.answer)
          })
        } else { //单选考试模式
          this.setData({ //设置隐藏答案
            showTip: false
          })
        }
      }
      console.log('你选了答案', this.data.currMultipleAnswer, this.data.currAnswer)
    }
  }
})